<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="单位" prop="unit">
        <el-input
          v-model="queryParams.unit"
          placeholder="请输入"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        <el-button type="primary" plain icon="Plus" @click="handleAdd"
          >新增</el-button
        >
        <!-- <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['platform:stat:edit']"
          >修改</el-button
        > -->
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
        <el-button type="warning" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-form-item>
    </el-form>

    <el-table
      v-loading="loading"
      :data="statList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="id" /> -->
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column
        label="公用配电线路总数"
        align="center"
        prop="totalLineNum"
      />
      <el-table-column
        label="一张图治理合格线路数(条)"
        align="center"
        prop="totalFullLineCj"
      />
      <el-table-column
        label="中压问题总数(项)"
        align="center"
        prop="totalFullLineLr"
      />
      <el-table-column label="治理合格率(%)" align="center" prop="fullLineCjl">
        <template #default="scope">
          <span>{{ scope.row.fullLineCjl }}%</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单线图推送合格率(%)"
        align="center"
        prop="pzImportNum"
      >
        <template #default="scope">
          <span>{{ scope.row.pzImportNum }}%</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="配白数据库关联数"
        align="center"
        prop="pzDbRelationNum"
      /> -->
       <el-table-column
        label="是否参与统计"
        align="center"
        prop="isStat"
      /> 
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding"
        width="200"
      >
        <template #default="scope">
          <el-button
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['platform:stat:edit']"
            >修改</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['platform:stat:remove']"
            >删除</el-button
          >
          <el-button type="text" @click="handleLook(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单位线路统计对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="statRef" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="单位" prop="unit">
          <el-tree-select
            v-model="form.unit"
            :data="deptOptions"
            :props="{ value: 'label', label: 'label', children: 'children' }"
            value-key="label"
            placeholder="请选择"
            check-strictly
            :disabled="formDisable"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="公用配电线路总数" prop="totalLineNum">
          <el-input
            v-model="form.totalLineNum"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="一张图治理合格线路数(条)" prop="totalFullLineCj">
          <el-input
            v-model="form.totalFullLineCj"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="中压问题总数(项)" prop="totalFullLineLr">
          <el-input
            v-model="form.totalFullLineLr"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="治理合格率(%)" prop="fullLineCjl">
          <el-input
            v-model="form.fullLineCjl"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>
        <el-form-item label="单线图推送合格率(%)" prop="pzImportNum">
          <el-input
            v-model="form.pzImportNum"
            placeholder="请输入"
            :disabled="formDisable"
          />
        </el-form-item>
        <!-- <el-form-item label="配白数据库关联数" prop="pzDbRelationNum">
          <el-input
            v-model="form.pzDbRelationNum"
            placeholder="请输入配白数据库关联数"
            :disabled="formDisable"
          />
        </el-form-item> -->
        <el-form-item label="是否参与统计" prop="isStat">
          <el-select
            v-model="form.isStat"
            placeholder="是否统计"
            clearable
            style="width: 100%"
          >
            <el-option
              v-for="dict in dl_ischange"
              :key="dict.label"
              :label="dict.label"
              :value="dict.label"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm" v-if="!formDisable"
            >确 定</el-button
          >
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stat">
import {
  listStat,
  getStat,
  delStat,
  addStat,
  updateStat,
} from "@/api/platform/unitLineStat.js";

import { deptTreeSelect } from "@/api/system/dept";

const { proxy } = getCurrentInstance();

const { dl_ischange } = proxy.useDict("dl_ischange");

const statList = ref([]);
const open = ref(false);
const formDisable = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const deptOptions = ref(undefined);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    unit: null,
    totalLineNum: null,
    totalFullLineCj: null,
    totalFullLineLr: null,
    fullLineCjl: null,
    pzImportNum: null,
    pzDbRelationNum: null,
  },
  rules: {},
});

const { queryParams, form, rules } = toRefs(data);

/** 查询单位线路统计列表 */
function getList() {
  loading.value = true;
  listStat(queryParams.value).then((response) => {
    statList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 查询部门下拉树结构 */
function getDeptTree() {
  deptTreeSelect().then((response) => {
    deptOptions.value = response.data;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  formDisable.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    unit: null,
    totalLineNum: null,
    totalFullLineCj: null,
    totalFullLineLr: null,
    fullLineCjl: null,
    pzImportNum: null,
    pzDbRelationNum: null,
    isStat: "是",
  };
  proxy.resetForm("statRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  formDisable.value = false;
  title.value = "添加单位线路统计";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getStat(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    formDisable.value = false;
    title.value = "修改单位线路统计";
  });
}

/** 查看按钮操作*/
function handleLook(row) {
  const _id = row.id || ids.value;
  getStat(_id).then((response) => {
    form.value = response.data;
    open.value = true;
    formDisable.value = true;
    title.value = "查看单位线路统计";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["statRef"].validate((valid) => {
    if (valid) {
      if (form.value.id != null) {
        updateStat(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStat(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal
    .confirm('是否确认删除单位线路统计编号为"' + _ids + '"的数据项？')
    .then(function () {
      return delStat(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "platform/stat/export",
    {
      ...queryParams.value,
    },
    `stat_${new Date().getTime()}.xlsx`
  );
}

getDeptTree();
getList();
</script>
